
package com.liyy.platform.service;
import com.liyy.platform.dao.ProductRepository;
import com.liyy.platform.domain.Goods;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 产品业务逻辑
 */


@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public void saveProduct(Goods goods){
        productRepository.saveProduct(goods);
    }

    public  void  deleteProduct(Long id){
        productRepository.deleteProduct(id);
    }

    //产品的查询及分页
    @SuppressWarnings("all")
    public List getProductList(String goodsName,String goodsBrand,String goodsAttr,int pageNo,int pageSize){
        SearchResponse response=null;
        List list=new ArrayList();
        if((goodsName==null||"".equals(goodsName))&&(goodsBrand==null||"".equals(goodsBrand))&&(goodsAttr==null||"".equals(goodsAttr))){
            response=productRepository.getAllList(pageNo,pageSize);
        }else{
            response=productRepository.getProductList(goodsName,goodsBrand,goodsAttr,pageNo,pageSize);
        }
        if(response!=null){
            SearchHits hits= response.getHits();
            for(SearchHit hit:hits){
                list.add(hit.getSourceAsMap());
            }
        }
        return list;
    }


}
